<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>任务调度 - FreeRTOS学习笔记</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f4f4f4;
        }

        header {
            background: linear-gradient(135deg, #2c3e50, #3498db);
            color: white;
            text-align: center;
            padding: 2rem 0;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        h1 {
            font-size: 2.5rem;
            margin-bottom: 0.5rem;
        }

        .subtitle {
            font-size: 1.2rem;
            opacity: 0.9;
        }

        nav {
            background-color: #2c3e50;
            padding: 0;
            position: sticky;
            top: 0;
            z-index: 100;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        nav ul {
            list-style: none;
            display: flex;
            justify-content: center;
            flex-wrap: wrap;
        }

        nav li {
            margin: 0;
        }

        nav a {
            display: block;
            color: white;
            text-decoration: none;
            padding: 1rem 1.5rem;
            transition: background-color 0.3s;
        }

        nav a:hover {
            background-color: #3498db;
        }

        .container {
            max-width: 1200px;
            margin: 2rem auto;
            padding: 0 2rem;
        }

        .content {
            background-color: white;
            border-radius: 8px;
            padding: 2rem;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        section {
            margin-bottom: 3rem;
        }

        h2 {
            color: #2c3e50;
            border-bottom: 2px solid #3498db;
            padding-bottom: 0.5rem;
            margin-bottom: 1.5rem;
        }

        h3 {
            color: #3498db;
            margin: 1.5rem 0 1rem;
        }

        h4 {
            color: #2c3e50;
            margin: 1rem 0 0.5rem;
        }

        p {
            margin-bottom: 1rem;
        }

        ul, ol {
            margin-left: 2rem;
            margin-bottom: 1rem;
        }

        li {
            margin-bottom: 0.5rem;
        }

        pre {
            background-color: #f8f9fa;
            border: 1px solid #e9ecef;
            border-radius: 4px;
            padding: 1rem;
            overflow-x: auto;
            margin: 1rem 0;
        }

        code {
            font-family: 'Courier New', monospace;
            background-color: #f8f9fa;
            padding: 0.2rem 0.4rem;
            border-radius: 3px;
            font-size: 0.9rem;
        }

        pre code {
            background-color: transparent;
            padding: 0;
        }

        .function-signature {
            background-color: #e8f4fc;
            border-left: 4px solid #3498db;
            padding: 1rem;
            margin: 1rem 0;
            border-radius: 0 4px 4px 0;
        }

        .footer {
            text-align: center;
            padding: 2rem;
            background-color: #2c3e50;
            color: white;
            margin-top: 2rem;
        }

        .nav-links {
            display: flex;
            justify-content: space-between;
            margin-top: 2rem;
        }

        .nav-links a {
            text-decoration: none;
            color: #3498db;
            font-weight: bold;
            padding: 0.5rem 1rem;
            border: 1px solid #3498db;
            border-radius: 4px;
        }

        .nav-links a:hover {
            background-color: #3498db;
            color: white;
        }

        @media (max-width: 768px) {
            nav ul {
                flex-direction: column;
            }

            .container {
                padding: 0 1rem;
            }

            h1 {
                font-size: 2rem;
            }
        }
    </style>
    <!-- 引入浮动笔记窗口脚本 -->
    <script src="float-notes.js"></script>
</head>
<body>
    <header>
        <h1>FreeRTOS学习笔记</h1>
        <p class="subtitle">第三章：任务调度</p>
    </header>

    <nav>
        <ul>
            <li><a href="index.html">主页</a></li>
            <li><a href="chapter1.html">基础知识</a></li>
            <li><a href="chapter2.html">任务管理</a></li>
            <li><a href="chapter3.html">任务调度</a></li>
            <li><a href="chapter4.html">内存管理</a></li>
            <li><a href="chapter5.html">队列</a></li>
            <li><a href="chapter6.html">信号量</a></li>
            <li><a href="chapter7.html">软件定时器</a></li>
            <li><a href="image_gallery.html">图片资源</a></li>
        </ul>
    </nav>

    <div class="container">
        <div class="content">
            <section id="task-scheduling">
                <h2>3. 任务调度</h2>
                
                <h3>3.1 启动调度器</h3>
                <p>启动调度器是FreeRTOS运行的起点：</p>
                <ol>
                    <li>创建空闲任务（必须存在）</li>
                    <li>如果启用软件定时器，创建定时器服务任务</li>
                    <li>初始化系统时钟和相关硬件</li>
                    <li>启动第一个就绪任务</li>
                </ol>
                
                <h3>3.2 任务切换机制</h3>
                
                <h4>上下文切换</h4>
                <p>任务切换的本质是上下文切换：</p>
                <ul>
                    <li><strong>保存现场</strong>: 将当前任务的寄存器状态保存到任务栈</li>
                    <li><strong>恢复现场</strong>: 从目标任务栈中恢复寄存器状态</li>
                    <li>通过PendSV中断实现，确保切换过程的原子性</li>
                </ul>
                
                <h4>MSP与PSP指针</h4>
                <p>ARM Cortex-M系列处理器提供两个栈指针：</p>
                <ul>
                    <li><strong>MSP（主栈指针）</strong>: 用于处理异常和内核操作</li>
                    <li><strong>PSP（进程栈指针）</strong>: 用于任务执行</li>
                    <li>在任务切换时正确管理这两个指针至关重要</li>
                </ul>
                
                <h3>3.3 调度相关函数</h3>
                
                <h4>vTaskStartScheduler - 启动调度器</h4>
                <div class="function-signature">
                    <pre><code>void vTaskStartScheduler(void);</code></pre>
                </div>
                
                <p><strong>作用</strong>：启动FreeRTOS任务调度器</p>
                
                <p><strong>示例</strong>：</p>
                <pre><code>int main(void) {
    // 初始化硬件
    // ...
    
    // 创建任务
    xTaskCreate(vTask1, "Task1", 128, NULL, 1, NULL);
    xTaskCreate(vTask2, "Task2", 128, NULL, 2, NULL);
    
    // 启动调度器
    vTaskStartScheduler();
    
    // 程序不会执行到这里
    for (;;);
}</code></pre>
                
                <h4>vTaskSuspend - 挂起任务</h4>
                <div class="function-signature">
                    <pre><code>void vTaskSuspend(TaskHandle_t xTaskToSuspend);</code></pre>
                </div>
                
                <p><strong>参数</strong>：</p>
                <ul>
                    <li>xTaskToSuspend: 要挂起的任务句柄，NULL表示挂起当前任务</li>
                </ul>
                
                <p><strong>作用</strong>：挂起指定的任务</p>
                
                <p><strong>示例</strong>：</p>
                <pre><code>// 挂起其他任务
vTaskSuspend(xOtherTaskHandle);

// 挂起当前任务
vTaskSuspend(NULL);</code></pre>
                
                <h4>vTaskResume - 恢复任务</h4>
                <div class="function-signature">
                    <pre><code>void vTaskResume(TaskHandle_t xTaskToResume);</code></pre>
                </div>
                
                <p><strong>参数</strong>：</p>
                <ul>
                    <li>xTaskToResume: 要恢复的任务句柄</li>
                </ul>
                
                <p><strong>作用</strong>：恢复被挂起的任务</p>
                
                <p><strong>示例</strong>：</p>
                <pre><code>// 恢复被挂起的任务
vTaskResume(xSuspendedTaskHandle);</code></pre>
            </section>
            
            <div class="nav-links">
                <a href="chapter2.html">上一章：任务管理</a>
                <a href="chapter4.html">下一章：内存管理</a>
            </div>
        </div>
    </div>

    <footer class="footer">
        <p>FreeRTOS学习笔记 &copy; 2025</p>
        <p>作者: zou.commit()</p>
    </footer>
</body>
</html>